<template>
	<view class="addressPage content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index">
			<view class="wrapper" @click="checkAddress(item)">
				<u-icon name="checkmark-circle-fill" color="#0157FA" v-if="item.id == addressId" class="tag"></u-icon>
				<!-- <u-icon name="minus-circle" v-else="item.default" class="tag"></u-icon> -->
				<view class="circle-box" v-else>
					<view class="circle-unselected"></view>
				</view>
				<view style="margin-left:30rpx;">
					<view class="u-box">
						<text class="name">{{item.name}}</text>
						<text class="mobile">{{item.mobile}}</text>
						<text class="default-tag" v-if="item.default == 1">默认</text>
					</view>
					<view class="address-box">
						<text class="address">{{item.address}}</text>
					</view>
				</view>
			</view>
			<u-icon name="edit-pen" size="28" color="#333333" @click.stop="addAddress('edit', item)"></u-icon>
		</view>
		<button class="add-btn" @click="addAddress('add', null)">添加收货地址</button>
	</view>
</template>

<script>
	import { getUser } from '@/utils/vmall.js';
	export default {
		data() {
			return {
				source: 0,
				addressList: [],
				userInfo: {},
				addressId: {}
			}
		},
		onLoad(option){
			console.log(option.source);
			this.source = option.source;
			this.addressId = option.addressId
			console.log('onload detail')
			getUser().then(info => {
				this.userInfo = info
				this.getList()
			})
		},
		methods: {
			//选择地址
			checkAddress(item){
				this.addressList.forEach(aitem => {aitem.default = false})
				item.default = !item.default
				const title = "已选择"
				this.$appFunc.msg(title)
				if(this.source == 1){
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$appFunc.prePage().address = item;
					uni.navigateBack()
				}
			},
			getList() {
				this.$request.get(this.$api.addressList, {
					userId: this.userInfo.id
				}).then(res => {
					// 加载状态重置
					uni.hideLoading()
					const { success, result } = res
					if (success) {
						const records = result.records.filter(item => {return item.userId == this.userInfo.id})
						
						const addressList = records.map(item => {
							return {
								id: item.id,
								name: item.name,
								mobile: item.mobile,
								address: item.address,
								default: item.isDefault == 0 ? false : true,
							}
						})
						this.addressList = addressList
					}  else {
						this.goodsList = []
						this.$appFunc.msg('获取地址失败')
					}
				})
			},
			addAddress(type, item){
				uni.navigateTo({
					url: `/pages/newAddress/newAddress?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			goAdd() {
				uni.navigateTo({
					url: `/pages/newAddress/newAddress?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			//添加或修改成功之后回调
			refreshList(data, type){
				this.getList()
				// console.log(data, type);
			},
			
		}
	}
</script>

<style lang='scss'>
	@import "@/style/variable.scss";
	page{
		padding-bottom: 120upx;
	}
	.addressPage {
		.content{
			position: relative;
		}
		.list{
			display: flex;
			align-items: center;
			padding: 20rpx 50rpx;
			background: #fff;
			position: relative;
			box-shadow: 0 2rpx 15rpx 0 rgba(219, 219, 219, 0.5);
			margin:20rpx 30rpx;
			border-radius: 16rpx;
		}
		.wrapper{
			display: flex;
			flex: 1;
		}
		.circle-box {
			display: flex;
			flex-direction: row;
			align-items: center;
			padding: 0px 1px;
			.circle-unselected {
				width: 14px;
				height: 14px;
				border: 1px solid gray;
				border-radius: 50%;
				box-sizing: border-box;
			}
		}
		
		.address-box{
			display: flex;
			align-items: center;
			margin:12rpx 0 6rpx;
			.tag{
				font-size: 24upx;
				color: $base-color;
				margin-right: 10upx;
				background: #fffafb;
				border: 1px solid #ffb4c7;
				border-radius: 4upx;
				padding: 4upx 10upx;
				line-height: 1;
			}
			.address{
				font-size: 30upx;
				color: $font-color-dark;
			}
		}
		.u-box{
			font-size: 28upx;
			color:#333333;
			margin-top: 16upx;
			.name{
				margin-right: 30upx;
			}
			.default-tag {
				padding: 0 10rpx;
				line-height: 1;
				border: 1px solid $uni-color-primary;
				color: $uni-color-primary;
				font-size: 24upx;
				border-radius: 6rpx;
				margin-left: 10rpx;
			}
		}
		.icon-bianji{
			display: flex;
			align-items: center;
			height: 80upx;
			font-size: 40upx;
			color: $font-color-light;
			padding-left: 30upx;
		}
		.add-btn{
			display:block;
			width: 690upx;
			height: 80upx;
			position: fixed;
			left: 30rpx;
			right: 30rpx;
			bottom: 16rpx;
			background-color: $base-color;
			border-radius: 10upx;
			color:$font-color-white;
			font-size:$font-lg;
		}
		
	}
</style>
